package com.hdugraduate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SearchResultList extends ListActivity {
	private ArrayList searchResult = new ArrayList();
	
	String selectWhat = null;
	String tempResult = null;
	int resultCount = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Bundle searchBundle = this.getIntent().getExtras();
		//selectWhat = searchBundle.getString("selectItems");
		File search = new File ("sdcard/TN/DB/HangZhou.txt");
		try {
			BufferedReader searchBuffer = new BufferedReader(new FileReader(search));
			tempResult = searchBuffer.readLine();
			while(tempResult != null){
				if('A'== tempResult.charAt(0)){
					int signStart = 0;
					int signEnd = 0;
					signStart = tempResult.indexOf("<", signEnd);
					signEnd = tempResult.indexOf(">",signEnd);
					String name = tempResult.substring(signStart+1, signEnd-1);
					searchResult.add(name);
					resultCount=resultCount+1;
				}
				tempResult = searchBuffer.readLine();
				tempResult = searchBuffer.readLine();
			}
			if(resultCount == 0)searchResult.add("noting");
			searchBuffer.close();
		}catch (IOException e){Toast.makeText(SearchResultList.this,"Got None data", Toast.LENGTH_SHORT).show();};
		this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, searchResult));
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		File search_click = new File ("sdcard/TN/DB/HangZhou.txt");
		String valuesWant = null;
		try {
			BufferedReader searchBufferClick = new BufferedReader(new FileReader(search_click));
			String tempResultClick = searchBufferClick.readLine();
			while(tempResultClick != null){
				String nameClick = null;
				if('A'== tempResultClick.charAt(0)){
					int signStartClick = 0;
					int signEndClick = 0;
					signStartClick = tempResultClick.indexOf("<", signEndClick);
					signEndClick = tempResultClick.indexOf(">",signEndClick);
					nameClick = tempResultClick.substring(signStartClick+1, signEndClick-1);
				}
				valuesWant = searchResult.get(position).toString();
				if(nameClick.equals(valuesWant)){
					valuesWant = tempResultClick;
					break;
				}
				tempResultClick = searchBufferClick.readLine();
				tempResultClick = searchBufferClick.readLine();
			}
		}catch (IOException e){Toast.makeText(SearchResultList.this,"Got None data", Toast.LENGTH_SHORT).show();};
		Intent viewMore = new Intent(SearchResultList.this,ViewResultMore.class);
		Bundle listToMore = new Bundle();
		listToMore.putString("result", valuesWant);
		viewMore.putExtras(listToMore);
		startActivity(viewMore);		
	} 
}
